package com.cn.jinl.service;

import com.cn.jinl.api.IViolationService;
import com.cn.jinl.dao.ViolationMapper;
import com.cn.jinl.domain.Violation;
import com.cn.jinl.domain.ViolationExample;
import com.cn.jinl.utils.*;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * Created On : 2024/1/8.
 * <p>
 * Author : Huiyan
 * <p>
 * Description: ViolationServiceImpl
 */
@Service
public class ViolationServiceImpl implements IViolationService {

    @Autowired
    private ViolationMapper violationMapper;

    @Autowired
    private GenerateUUID generateUUID;


    @Override
    public List<Violation> getPendingApprovalList(QueryForm queryForm) throws Exception {

        List<Violation> list = violationMapper.getPendingApprovalList(queryForm);

        return list;
    }

    @Override
    public List<Violation> getApprovedList(QueryForm queryForm) throws Exception {

        List<Violation> list = violationMapper.getApprovedList(queryForm);

        return list;
    }

    @Override
    public List<Violation> getApprovedRecentList(QueryForm queryForm) throws Exception {

        List<Violation> list = violationMapper.getApprovedRecentList(queryForm);

        return list;
    }

    @Override
    public String addViolation(Violation violation) throws Exception {

        if (null == violation){
            return "请添加罚单内容";
        }

        //determine whether input data is legal
        String result = this.violationLeagal(violation);
        if (!"".equals(result)){
            return result;
        }

        //generate id
        String id = CommonUtil.generateRandomNum("fd_");
        violation.setId(id);

        //generate fd_code  ep:FD230101
        String key = "FD"+generateUUID.getYearMoth();
        int num = generateUUID.getNumberFlag(key);
        String code = key+generateUUID.getTwoNumStr(num);
        violation.setFdCode(code);

        //default fd_status
        violation.setFdStatus("0");

        int count = violationMapper.insertSelective(violation);

        return CommonUtil.outStr(count);
    }

    @Override
    public String updateViolation(Violation violation) throws Exception {

        String id = violation.getId();
        if (StringUtils.isEmpty(id)){
            return "id is null";
        }

        String result = this.violationLeagal(violation);
        if (!"".equals(result)){
            return result;
        }

        ViolationExample example = new ViolationExample();
        example.createCriteria().andIdEqualTo(id).andStatusEqualTo(Constants.NORMAL_STATUS);
        int count = violationMapper.updateByExampleSelective(violation,example);

        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteViolation(QueryForm queryForm) throws Exception {

        List<String> ids = queryForm.getIds();
        if (CollectionsUtil.isEmpty(ids)){
            return "delete id is null";
        }

        Violation violation = new Violation();
        violation.setStatus(Constants.DEL_STATUS);
        ViolationExample example = new ViolationExample();
        example.createCriteria().andIdIn(ids);
        int count = violationMapper.updateByExampleSelective(violation,example);

        return CommonUtil.outStr(count);
    }

    @Override
    public String updateFdStatus(Violation violation) throws Exception {

        String id = violation.getId();
        if (StringUtils.isEmpty(id)){
            return "id is null";
        }

        int count = violationMapper.updateFdStatus(violation);

        return CommonUtil.outStr(count);
    }

    @Override
    public int getViolationApproveCount() throws Exception {
        ViolationExample example = new ViolationExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andFdStatusEqualTo("0");
        return violationMapper.countByExample(example);
    }

    @Override
    public Map<String, Object> countViolationToday() throws Exception {

        Map<String,Object> map = Maps.newHashMap();

        Integer count = violationMapper.countViolationToday();

        map.put("violationCount",count);

        return map;
    }

    @Override
    public List<Violation> currentFiveViolation() throws Exception {

        List<Violation> list = violationMapper.selectNewFiveViolation();
        for (Violation v : list){
            String dateString = v.getFdApproveTime().substring(0,11);
            v.setFdApproveTime(dateString);

            String dataonly = v.getCreateTimeFt().substring(5,10);
            String timeonly = v.getCreateTimeFt().substring(11,16);

            v.setDataonly(dataonly);
            v.setTimeonly(timeonly);
        }

        return list;
    }




    private String violationLeagal(Violation violation){
        String result ="";

        if (StringUtils.isBlank(violation.getFdType())){
            result = "请选择违章类型";
            return result;
        }

        if (StringUtils.isBlank(violation.getFdDate())){
            result = "请选择违章日期";
            return result;
        }

        if (StringUtils.isBlank(violation.getFdViolatorId())){
            result = "请选择违章人";
            return result;
        }

        if (StringUtils.isBlank(violation.getFdReason())){
            result = "请输入违章事由";
            return result;
        }

        if (StringUtils.isBlank(violation.getFdMoney())){
            result = "请输入处罚金额";
            return result;
        }

        if (!CommonUtil.isNumber(violation.getFdMoney())){
            result = "输入参数有误！";
            return result;
        }


        return result;

    }


}
